package BitCreekTracker; /**
 * @author Ivan Litsvinenka & Emanuele Ricci
 * Classe che implementa il comportamento del Thread del Tracker che gestisce le connessioni TCP relative a un torrent
 *
 */

import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class TrackerTCPThread implements Runnable {

    private final ExecutorService workers;

    /**
     * Porta TCP su cui viene bindato il Tracker
     */
    private final int port;

    /**
     * Costruttore di TrackerTCPThread
     * @param port
     */
    public TrackerTCPThread ( int port ) {
        this.port = port;
        workers = Executors.newCachedThreadPool ();
    }

    public void run () {
        try {
            SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
            SSLServerSocket sslserversocket = (SSLServerSocket) sslserversocketfactory.createServerSocket(this.port);

            

            while ( true ) {

                SSLSocket sslsocket = (SSLSocket) sslserversocket.accept();

                workers.execute(new TrackerTCPWorker(sslsocket));

            }
        } catch ( Exception e ) {
	    	e.printStackTrace();
	    }

	}
}
